#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author: caijj
@version: V1.0
@file: zg_sz_second_house_detail_page.py
@time: 2022/02/18
"""
import ast
import multiprocessing
import uuid
from time import sleep
import requests
from fake_useragent import UserAgent
from lxml import etree
from selenium import webdriver
from tools import timeutil
from tools.database_tool import DataBaseTool
from tools.decorator import update_flag

time = timeutil.dt_strptime("%Y-%m-%d %H%M%S")


def agent():
    ua = UserAgent(verify_ssl=False)
    headers = {'User-Agent': ua.random}
    return headers


def chrome_enter_target_page(url='https://su.xinfang.zhuge.com/'):  # chrome进入目标页面
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    web_driver = webdriver.Chrome(options=chrome_options)
    web_driver.maximize_window()
    web_driver.get(url)
    return web_driver


# def get_second_house_location2(index=0):  # 页面获取房源经纬度
#     web_driver = None
#     second_house_list = get_second_house_detail_link()
#     time = timeutil.dt_strptime("%Y-%m-%d %H%M%S")
#     try:
#         for second_house_info in second_house_list[index:]:
#             house_detail_link = second_house_info[1]
#             position_info = second_house_info[2]
#             house_code = house_detail_link.split('/')[-2][1:]
#             url = 'https://su.esfxiaoqu.zhuge.com/' + house_code + '/'
#             web_driver = chrome_enter_target_page(url)
#             sleep(4)
#             html_source = web_driver.page_source  # 该属性可以获取当前浏览器的当前页的源码（html）
#             tree = etree.HTML(html_source)
#             lng = tree.xpath("//input[@id='areaLoc']")[0].get('value')
#             lng_dict = ast.literal_eval(lng)
#             record_id = uuid.uuid4()
#             insert_second_house_location_data((record_id, position_info, lng_dict['lng'], lng_dict['lat'], time))
#             web_driver.quit()
#             index += 1
#     except Exception as e:
#         print(e)
#         print(house_detail_link)
#         web_driver.quit()
#         sleep(10)
#         get_second_house_location2(index)


def get_xiaoqu_location(xiaoqu_list, index=0):  # 百度地图api获取经纬度
    try:
        for xiaoqu_info in xiaoqu_list[index:]:
            record_id = uuid.uuid4()
            xiaoqu_id = xiaoqu_info[0]
            xiaoqu_name = xiaoqu_info[1]
            region = xiaoqu_info[2]
            district = xiaoqu_info[3]
            house_address = xiaoqu_info[4]
            location = geocodeB('苏州' + ' ' + region + ' ' + district + ' ' + xiaoqu_name)
            insert_xiaoqu_location_data(
                (record_id, house_address, location[0], location[1], time, region, district, xiaoqu_name, xiaoqu_id))
            index += 1
    except Exception as e:
        print(e)
        print(str(xiaoqu_id) + '' + region + ' ' + district + ' ' + xiaoqu_name + ' ' + location[0])
        sleep(10)
        get_xiaoqu_location(index)


# def get_rent_house_location(index=0):
#     rent_house_list = get_rent_house_detail_link()
#     time = timeutil.dt_strptime("%Y-%m-%d %H%M%S")
#     try:
#         for rent_house_info in rent_house_list[index:]:
#             record_id = uuid.uuid4()
#             region = rent_house_info[2]
#             district = rent_house_info[3]
#             position_info = rent_house_info[4]
#             location = geocodeB('苏州' + ' ' + region + ' ' + district + ' ' + position_info)
#             insert_rent_house_location_data((record_id, region, district, position_info,
#                                             location[0], location[1], time))
#             index += 1
#     except Exception as e:
#         print(e)
#         print(region + ' ' + district + ' ' + position_info)
#         sleep(10)
#         get_second_house_location(index)


def geocodeB(address):
    """
    @ address: 名称字符串
    @ 返回值：经度，纬度
    """
    base_url = "http://api.map.baidu.com/geocoder?address={address}&output=json&key=9NFsYXeiyhl6rXnCDQbetjZYW4tLFDo5". \
        format(address=address)
    response = requests.get(base_url)
    answer = response.json()
    latitude = answer['result']['location']['lng']
    longitude = answer['result']['location']['lat']
    return latitude, longitude


def get_xiaoqu_info(process_count='', table_name='mega_zg_sz_xiaoqu_info'):
    # sql = "select id,detail_url,region, district,position_info from (select ROW_NUMBER() over(partition by region, " \
    #       "district, position_info order by id) as RowId,* from {}) x where x.RowId=1".format(table_name)
    data_list = []
    sql = "select id,xiaoqu_name,region,district,house_address from {}".format(table_name)
    db = DataBaseTool()
    result = db.select_sql(sql)
    # step = len(result) // process_count
    # # remainder = len(result) % process_count
    # for i in range(1, process_count + 1):
    #     if i == range(1, process_count + 1)[-1]:
    #         data = result[step * (i - 1):]
    #     else:
    #         data = result[step * (i - 1):step * i]
    #     data_list.append(data)
    # return data_list
    return result


def insert_xiaoqu_location_data(data, table_name='mega_zg_sz_xiaoqu_location_info'):
    db = DataBaseTool()
    sql = "insert into {}(id, position_info, longitude, latitude, create_time, region, district, xiaoqu_name, " \
          "xiaoqu_id) values(%s,%s,%s,%s,%s,%s,%s,%s,%s)".format(table_name)
    try:
        cursor = db.conn.cursor()
        # cursor.executemany(sql, data)
        cursor.execute(sql, data)
        db.conn.commit()
    except Exception as e:
        print(e)
    db.conn.close()
    # db = DataBaseTool()
    # sql = "update {} set flag = 1 where tbname = '{}' and flag = 0".format('mega_syn_hand', table_name)
    # db.update_sql(sql)


# def get_rent_house_detail_link(table_name='mega_zg_sz_zf_estate_info'):
#     sql = "select id,detail_url,region, district,position_info from (select ROW_NUMBER() over(partition by region, " \
#           "district, position_info order by id) as RowId,* from {}) x where x.RowId=1".format(table_name)
#     db = DataBaseTool()
#     result = db.select_sql(sql)
#     return result
#
#
# @update_flag('mega_syn_hand', 'mega_zg_sz_zf_estate_location_info')
# def insert_rent_house_location_data(data, table_name='mega_zg_sz_zf_estate_location_info'):
#     db = DataBaseTool()
#     sql = "insert into {}(id, region, district, position_info, longitude, latitude, create_time) " \
#           "values(%s,%s,%s,%s,%s,%s,%s)".format(table_name)
#     try:
#         cursor = db.conn.cursor()
#         # cursor.executemany(sql, data)
#         cursor.execute(sql, data)
#         db.conn.commit()
#     except Exception as e:
#         print(e)
#     # db.conn.close()
#     # db = DataBaseTool()
#     sql = "update {} set flag = 1 where tbname = '{}' and flag = 0".format('mega_syn_hand', table_name)
#     db.update_sql(sql)


def del_data(table_name):
    db = DataBaseTool()
    sql = "delete from {}".format(table_name)
    db.update_sql(sql)


@update_flag('mega_syn_hand', 'mega_zg_sz_xiaoqu_location_info')
def get_xiaoqu_location_data(table_name='mega_zg_sz_xiaoqu_location_info'):
    del_data(table_name)
    xiaoqu_info_list = get_xiaoqu_info()
    get_xiaoqu_location(xiaoqu_info_list)

    # process_count = multiprocessing.cpu_count()
    # pool = multiprocessing.Pool(multiprocessing.cpu_count())
    #
    # xiaoqu_info_list = get_xiaoqu_info(process_count)
    #
    # pool.map(get_xiaoqu_location, xiaoqu_info_list)
    # pool.close()
    # pool.join()

    # db = DataBaseTool()
    # sql = "update {} set flag = 1 where tbname = '{}' and flag = 0".format('mega_syn_hand',
    #                                                                        table_name)
    # db.update_sql(sql)

# def get_total_pages(target_link, page_number, area_no, decoration, house_use_type, view_state, event_validation,
#                     build_area):
#     # 通过字典方式定义请求body
#     tb = ''
#     total_pages = ''
#     FormData = {
#         '__EVENTTARGET': '',
#         '__EVENTARGUMENT': '',
#         '__LASTFOCUS': '',
#         'ctl00$MainContent$txt_Pro': '',
#         'ctl00$MainContent$txt_Com': '',
#         'ctl00$MainContent$txt_Price1': '',
#         'ctl00$MainContent$txt_Price2': '',
#         'ctl00$MainContent$txt_Area1': build_area['start'],
#         'ctl00$MainContent$txt_Area2': build_area['end'],
#         'ctl00$MainContent$PageGridView1$ctl22$PageList': page_number,
#         "ctl00$MainContent$ddl_qy": area_no,
#         'ctl00$MainContent$bt_select': '查询',
#         'ctl00$MainContent$ddl_houseclass': house_use_type,
#         'ctl00$MainContent$rb_HF_CODE': decoration,
#         '__VIEWSTATEGENERATOR': '4A995636',
#         '__VIEWSTATE': view_state,
#         '__EVENTVALIDATION': event_validation
#     }
#     data = parse.urlencode(FormData)


if __name__ == '__main__':
    get_xiaoqu_location_data()
